import store from '@/store';
/**
 * 获取用户权限
 * 输入: [{id:1, url: '/url1', authCode: 'user:add'}]
 * 输出: {"user:add": true, "user:update": true}
 */
export function userPermissions (userAuth) {
  let result = getPermissions(Array.from(userAuth));
  store.dispatch('setUserAuth', result);
}

function getPermissions (arr) {
  let tmp = {};
  arr.forEach(item => {
    if (item.url) {
      tmp[item.authCode] = true;
    }
    if (item.children && item.children.length > 0) {
      let childs = getPermissions(item.children);
      tmp = {...tmp, ...childs};
    }
  });
  return tmp;
}

export function hasPermission (authCode) {
  return store.getters.auth[authCode];
}
